package cn.edu.njuit.springbootfilmreview.controller;

import cn.edu.njuit.springbootfilmreview.dto.MovieInfo;
import cn.edu.njuit.springbootfilmreview.service.MovieInfoService;
import cn.edu.njuit.springbootfilmreview.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static cn.edu.njuit.springbootfilmreview.utils.ResultCode.PARAM_NOT_COMPLETE;
import static cn.edu.njuit.springbootfilmreview.utils.ResultCode.RESULT_CODE_DATA_NONE;

/**
 * @Author guzy
 * @Description 电影接口
 * @Date 2022/5/23 20:26
 **/

@Api(value = "电影信息接口", tags = "电影信息接口")
@RestController
@RequestMapping("api/movie-info")
public class MovieInfoController {

    @Autowired
    MovieInfoService movieInfoService;

    @ApiImplicitParams({
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true)}
    )
    @ApiOperation(value = "根据评分降序,分页获取电影")
    @GetMapping("/{currentPage}/{pageSize}/rating")
    public Result getPageAllMovieInfoByRating(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize) {
        if (currentPage == null || String.valueOf(pageSize) == null) {
            return Result.failure(PARAM_NOT_COMPLETE);
        }
        IPage<MovieInfo> pageMovieInfoByRating = movieInfoService.getPageMovieInfoByRating(currentPage, pageSize);
        if (pageMovieInfoByRating.getRecords().size() > 0) {
            return Result.success(pageMovieInfoByRating);
        } else {
            return Result.failure(RESULT_CODE_DATA_NONE);
        }
    }

    @ApiImplicitParam(value = "电影id", name = "movieId", dataType = "Long", required = true)
    @ApiOperation(value = "获取电影详情信息")
    @GetMapping("/{movieId}")
    public Result getMovieInfoByMovieId(@PathVariable Long movieId) {
        if (movieId == null) {
            return Result.failure(PARAM_NOT_COMPLETE);
        }
        MovieInfo movieInfoByMovieId = movieInfoService.getMovieInfoByMovieId(movieId);
        if (movieInfoByMovieId != null) {
            return Result.success(movieInfoByMovieId);
        } else {
            return Result.failure(RESULT_CODE_DATA_NONE);
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true),
            @ApiImplicitParam(value = "电影类型", name = "movieType", dataType = "String"),
            @ApiImplicitParam(value = "电影年份", name = "movieYear", dataType = "int"),
            @ApiImplicitParam(value = "关键字", name = "keywords", dataType = "String")}
    )
    @ApiOperation(value = "根据电影或者年份类型获取电影,年份,关键字或类型不填,默认获取全部")
    @GetMapping("/{currentPage}/{pageSize}/classify")
    public Result getPageMovieInfoByMovieType(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize,
            @RequestParam(required = false, defaultValue = "") String movieType,
            @RequestParam(required = false, defaultValue = "") Integer movieYear,
            @RequestParam(required = false, defaultValue = "") String keywords) {
        //判断两个必填参数不能为空
        if (currentPage == null || String.valueOf(pageSize) == null) {
            return Result.failure(PARAM_NOT_COMPLETE);
        }
        //判断 电影类型，电影年份，关键字 不为空
        if (!Objects.equals(movieType, "") && movieYear != null && !Objects.equals(keywords, "")) {
            IPage<MovieInfo> movieInfoIPage = movieInfoService.getMovieInfoByMovieTypeAndMovieYearAndKeyWords(currentPage, pageSize, movieType, movieYear, keywords);
            if (movieInfoIPage.getRecords().size() > 0) {
                return Result.success(movieInfoIPage);
            } else {
                return Result.failure(RESULT_CODE_DATA_NONE);
            }
            //判断 电影类型 电影年份 不为空
        } else if (!Objects.equals(movieType, "") && movieYear != null) {
            IPage<MovieInfo> movieInfoByMovieTypeAndMovieYear = movieInfoService.getMovieInfoByMovieTypeAndMovieYear(currentPage, pageSize, movieType, movieYear);
            if (movieInfoByMovieTypeAndMovieYear.getRecords().size() > 0) {
                return Result.success(movieInfoByMovieTypeAndMovieYear);
            } else {
                return Result.failure(RESULT_CODE_DATA_NONE);
            }
            //判断 电影类型 不为空
        } else if (!Objects.equals(movieType, "")) {
            IPage<MovieInfo> pageMovieInfoByMovieType = movieInfoService.getPageMovieInfoByMovieType(currentPage, pageSize, movieType);
            if (pageMovieInfoByMovieType.getRecords().size() > 0) {
                return Result.success(pageMovieInfoByMovieType);
            } else {
                return Result.failure(RESULT_CODE_DATA_NONE);
            }
            //判断 电影年份 不为空
        } else if (movieYear != null) {
            IPage<MovieInfo> pageMovieInfoByMovieYear = movieInfoService.getPageMovieInfoByMovieYear(currentPage, pageSize, movieYear);
            if (pageMovieInfoByMovieYear.getRecords().size() > 0) {
                return Result.success(pageMovieInfoByMovieYear);
            } else {
                return Result.failure(RESULT_CODE_DATA_NONE);
            }
            //判断 关键字 不为空
        } else if (!Objects.equals(keywords, "")) {
            IPage<MovieInfo> pageMovieInfoByName = movieInfoService.getPageMovieInfoByName(currentPage, pageSize, keywords);
            if (pageMovieInfoByName.getRecords().size() > 0) {
                return Result.success(pageMovieInfoByName);
            } else {
                return Result.failure(RESULT_CODE_DATA_NONE);
            }
            //判断 如果 只填写 分页两个参数 就默认查找全部数据
        } else {
            IPage<MovieInfo> pageAllMovieInfo = movieInfoService.getPageAllMovieInfo(currentPage, pageSize);
            if (pageAllMovieInfo.getRecords().size() > 0) {
                return Result.success(pageAllMovieInfo);
            } else {
                return Result.failure(RESULT_CODE_DATA_NONE);
            }
        }
    }


}
